import java.util.HashMap;

public class Main1 {
    public static void main(String[] args) {
        System.out.println(isValid("[{]]"));
        System.out.println(isValid("))(("));
        System.out.println(isValid("(())"));
    }

    public static boolean isValid(String s) {
        //边界
        if(s == null ||s.length() == 0) {
            return true;
        }
        //初步判断
        if(s.length() % 2 == 1) return false;
        HashMap<Character,Character> map = new HashMap<>();
        //字符串模拟栈的好处
        StringBuilder st = new StringBuilder();
        map.put('}','{');map.put(']','[');map.put(')','(');
        for(char x : s.toCharArray()) {
            if(map.containsValue(x)) {
                //左括号
                st.append(x);
            } else {
                //右括号
                if(st.length() > 0 && map.get(x) == st.charAt(st.length()-1)) {
                    //匹配
                    st.deleteCharAt(st.length()-1);
                } else {
                    //不匹配
                    return false;
                }
            }
        }

        return st.length()==0;
    }
}
